﻿var MobilePage = function(pageContainer) {
    MobilePage.superclass.constructor.call(this, pageContainer);

    var self = this;
    var actionsDialog = null;
    var marginTop = 0;

    var Elements = {};
    this.Actions = [];
    this.IsExitPoint = false;
    this.LastPageHash = null;
    this.IsVisible = false;

    this.Rights = {
    
    };

    this.showActions = function() {
        if (!self.Actions.length)
            return;

        if (self.isAnyDialogOpen())
            self.hideAllDialogs();

        if (self.Actions.length == 1) {
            var a = self.Actions[0];
            if (a.Callback)
                a.Callback();
        } else {
            if (!actionsDialog) {
                $("body").append("<div class='actionsDialog dialog' style='position: inherit;'></div>");
                actionsDialog = $(".actionsDialog");
                actionsDialog.dialog({ 'templateId': 'actionsDialogTemplate' });
            }

            if (actionsDialog.attr("visible") == 'visible') {
                $("#actionsOverlay").hide();
                return actionsDialog.dialog('hide');
            }

            var actions = [];
            for (var i = 0; i < self.Actions.length; i++) {
                var a = self.Actions[i];
                if (!a.CanExecute || (a.CanExecute && a.CanExecute()))
                    actions.push(a);
            }

            if (!actions.length)
                return;

            actionsDialog.unbind();

            actionsDialog.bind('onShow', function() {
                $(this).attr('visible', 'visible');

                actionsDialog.find('.action').unbind().tap(function(e) {
                    var index = $(this).parent().children().index($(this));
                    actionsDialog.dialog('hide');
                    var a = actions[index];
                    if (a.Callback)
                        a.Callback();
                    $("#actionsOverlay").hide();

                    return Event.stopEvent(e);
                });
            }).bind('onHide', function() { $(this).removeAttr('visible'); });

            actionsDialog.dialog('show', { Actions: actions });
            $("#actionsOverlay").show().unbind().bind('click', function() {
                $(this).hide();
                actionsDialog.dialog('hide');
            });
        }
    };

    this.hideActions = function() {
        if (actionsDialog && actionsDialog.is(":visible"))
            actionsDialog.dialog('hide');

        $("#actionsOverlay").hide();
    };

    var initializePullToRefresh = function() {
        if (pageContainer.attr('data-pull-to-refresh') != 'true')
            return;

        pageContainer.bind('onPageHide', function() {
            self.IsVisible = false;

            if (pageContainer.attr('data-pull-to-refresh') == 'true')
                pageContainer.find('.pull_to_refresh').removeClass('active');
        });

        pageContainer.bind('onPageShow', function() {
            self.IsVisible = true;

            if (pageContainer.attr('data-pull-to-refresh') == 'true')
                pageContainer.find('.pull_to_refresh').addClass('active');
        });

        pageContainer.find('div.content').pull_to_refresh({
            refresh: function(callback) {
                if (callback)
                    callback();
            }
        });

        pageContainer.find('div.content').css('min-height', $(window).height());

        Elements.pullToRefresh = pageContainer.find(".pull_to_refresh");
        marginTop = parseInt(Elements.pullToRefresh.css('margin-top'), 10);
    };

    var initializeDynamicLoading = function() {
        if (pageContainer.attr('data-dynamic-loading') != 'true')
            return;

        var error = 10;

        $(window).scroll(function() {
            var pastBottom = $(window).scrollTop() + $(window).height() - marginTop >= document.body.scrollHeight - error;
            if (pastBottom && self.IsVisible)
                self.getPullUpData();
        });
    };

    this.getPullDownData = function() {};

    this.getPullUpData = function() {};

    var initializeOverlay = function() {
        pageContainer.append('<div class="page-overlay"></div>');
        Elements.pageOverlay = pageContainer.find('.page-overlay');

        Elements.pageOverlay.tap(function(e) {
            return Event.stopEvent(e);
        });

        Elements.pageOverlay.bind('click mousedown', function(e) {
            return Event.stopEvent(e);
        });

        pageContainer.bind('onPageShow', function() {
            setTimeout(function() {
                Elements.pageOverlay.hide();
            }, 500);
        });

        pageContainer.bind('onPageHide', function() {
            Elements.pageOverlay.show();
        });
    };

    this.initialize = override(self.initialize, function() {
        initializeOverlay();

        pageContainer.bind('onPageChanged', function() {
            app.MessagesFactory.hideToast();
            app.setPageTitle();
        });

        initializePullToRefresh();
        initializeDynamicLoading();
    });

    this.isAnyDialogOpen = function() {
    };

    this.hideAllDialogs = function() {
    };

    this.goBack = function() {
        if (app.MessagesFactory.isDialogVisible())
            return app.MessagesFactory.hideDialog();
        app.changePage(app.getPageByHash(self.LastPageHash));
    };
};
extend(MobilePage, Page);